<?php
// +----------------------------------------------------------------------
// | [RhaPHP System] Copyright (c) 2017 http://www.rhaphp.com/
// +----------------------------------------------------------------------
// | [RhaPHP] 并不是自由软件,你可免费使用,未经许可不能去掉RhaPHP相关版权
// +----------------------------------------------------------------------
// | Author: Geeson <qimengkeji@vip.qq.com>
// +----------------------------------------------------------------------

namespace addons\weiweb\controller;

use think\Db;
use app\common\controller\Addon;

class Index extends Addon
{

    public function show()
    {

        $url = getHostDomain() . addonUrl('subfms/index', ['id' => input('id')]);
        $getQrUrl = getHostDomain() . addonUrl('preview/qrcode').'?url='.urlencode($url);
        $this->assign('url', $url);
        $this->assign('getQrUrl',$getQrUrl);
        $this->fetch();
    }

    public function qrcode()
    {
        $url = input('url');
        header("Content-type: image/png");
        $url = urldecode($url);
        include_once EXTEND_PATH.'phpqrcode/phpqrcode.php';
        \Qrcode::png($url, $file = false, $level = 'L', $size = 4);
        exit();

    }

    function index(){
        $type = 1;
        $templetList = $this->getAllTemplet();
        $configs = isset($templetList[$type]) ? $templetList[$type] : array();
        $templet = Db::name('we_templet')->where(['mid'=>$this->mid,'type'=>$type])->find();
        if($templet && isset($configs[$templet['code']])){
            $templet_htm = $configs[$templet['code']]['value'];
        }else{
            $templet_htm = array_shift($configs);
            $templet_htm = isset($templet_htm['value']) ? $templet_htm['value'] : '';
        }
        if(!$templet_htm){
            $this->error('模板不存在');
        }

        $index = Db::name('we_index')->where(['mid'=>$this->mid])->find();
        $this->assign('index', $index);
        $columns = Db::name('we_columns')->where(['mid'=>$this->mid,'show'=>1])->order('sort ASC')->select();
        $articles = array();
        $cols_id = 0;
        if($templet_htm == 'templet1'){
            if(isset($columns[0]['id'])){
                $cols_id = $columns[0]['id'];
                $articles = Db::name('we_article')->where(['columns_id'=>$cols_id,'mid'=>$this->mid])->order('top DESC,add_time DESC')->limit(5)->select();
            }
        }

        $sliders = Db::name('we_slider')->where(['mid'=>$this->mid,'show'=>1,'columns_id'=>$cols_id])->order('sort ASC')->select();
        foreach ($articles as $k=>$article) {
            $time = time()-$article['add_time'];
            if($time <= 3600){
                $rang = intval($time/60)."分钟前";
            }else if($time <= 86400){
                $rang = intval($time/3600)."小时前";
            }else{
                $rang = intval($time/86400)."天前";
            }
            $articles[$k]['add_time'] = $rang;

        }

        $this->assign('sliders', $sliders);
        $this->assign('cols_id', $cols_id);
        $this->assign('articles', $articles);
        $this->assign('columns', $columns);

        $this->fetch('@index/'.$templet_htm);

    }

    function article(){
        $type = 3;
        $templetList = $this->getAllTemplet();
        $configs = isset($templetList[$type]) ? $templetList[$type] : array();
        $templet = Db::name('we_templet')->where(['mid'=>$this->mid,'type'=>$type])->find();
        if($templet && isset($configs[$templet['code']])){
            $templet_htm = $configs[$templet['code']]['value'];
        }else{
            $templet_htm = array_shift($configs);
            $templet_htm = isset($templet_htm['value']) ? $templet_htm['value'] : '';
        }
        if(!$templet_htm){
            $this->error('模板不存在');
        }

        $data = input();
        $id = isset($data['id']) ? $data['id'] : 0;

        $article = Db::name('we_article')->where(['id'=>$id])->find();
        if(!$article){
            $this->error('文章不存在');
        }
        $article['add_date'] = date('Y-m-d',$article['add_time']);
        Db::name('we_article')->where(['id'=>$id])->inc('views',1)->update();
        $index = Db::name('we_index')->where(['mid'=>$this->mid])->find();
        $this->assign('index', $index);
        $this->assign('article', $article);
        $this->fetch('@index/article/'.$templet_htm);
    }

    function columns(){
        $type = 2;
        $templetList = $this->getAllTemplet();
        $configs = isset($templetList[$type]) ? $templetList[$type] : array();
        $templet = Db::name('we_templet')->where(['mid'=>$this->mid,'type'=>$type])->find();
        if($templet && isset($configs[$templet['code']])){
            $templet_htm = $configs[$templet['code']]['value'];
        }else{

            $templet_htm = array_shift($configs);
            $templet_htm = isset($templet_htm['value']) ? $templet_htm['value'] : '';
        }
        if(!$templet_htm){
            $this->error('模板不存在');
        }

        $data = input();
        $cols_id = isset($data['id']) ? $data['id'] : 0;
        $index = Db::name('we_index')->where(['mid'=>$this->mid])->find();
        $this->assign('index', $index);
        $columns = Db::name('we_columns')->where(['mid'=>$this->mid,'show'=>1])->order('sort ASC')->select();

        $articles = Db::name('we_article')->where(['columns_id'=>$cols_id,'mid'=>$this->mid])->order('top DESC,add_time DESC')->limit(5)->select();
        $sliders = Db::name('we_slider')->where(['mid'=>$this->mid,'show'=>1,'columns_id'=>$cols_id])->order('sort ASC')->select();
        if(!$articles && !$sliders){
            $this->error('没有内容');
        }
        foreach ($articles as $k=>$article) {
            $time = time()-$article['add_time'];
            if($time <= 3600){
                $rang = intval($time/60)."分钟前";
            }else if($time <= 86400){
                $rang = intval($time/3600)."小时前";
            }else{
                $rang = intval($time/86400)."天前";
            }
            $articles[$k]['add_time'] = $rang;

        }


        $this->assign('sliders', $sliders);
        $this->assign('cols_id', $cols_id);
        $this->assign('articles', $articles);
        $this->assign('columns', $columns);



        $this->fetch('@index/columns/'.$templet_htm);

    }






}